﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using eCRAM.Model;
using eCRAM.Tools;

namespace eCRAM.WebSite.Models
{
    /// <summary>
    /// Modele pour la recherche de cram
    /// </summary>
    public class SearchCramModel
    {
        /// <summary>
        /// Critères de recherche
        /// </summary>
        public SearchCramCriteriasModel Criterias { get; set; }

        /// <summary>
        /// Resultats de la recherche
        /// </summary>
        public IEnumerable<SearchCramResultModel> Results { get; set; }
    }

    /// <summary>
    /// Modèle pour les critéres de recherche de crams
    /// </summary>
    public class SearchCramCriteriasModel
    {
        /// <summary>
        /// Mois recherché
        /// </summary>
        [DisplayName("Mois")]
        public int? Month { get; set; }
        /// <summary>
        /// Année recherchée
        /// </summary>
        [DisplayName("Année")]
        public int? Year { get; set; }
        /// <summary>
        /// Utilisateur recherché
        /// </summary>
        [DisplayName("Utilisateur")]
        public Guid? UserId { get; set; }
        /// <summary>
        /// Statut du cram recherché
        /// </summary>
        [DisplayName("Statut")]
        public int? Status { get; set; }
        /// <summary>
        /// Projet recherché
        /// </summary>
        [DisplayName("Projet")]
        public Guid? ActivityId { get; set; }
    }

    /// <summary>
    /// Modéle pour les résultats de la recherche
    /// </summary>
    public class SearchCramResultModel
    {
        /// <summary>
        /// Identifiant du cram
        /// </summary>
        public Guid Id { get; set; }
        /// <summary>
        /// Date de cram (mois année)
        /// </summary>
        public string Date { get; set; }
        /// <summary>
        /// Nom du propriétaire du cram
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// Liste des activités associées au cram
        /// </summary>
        public string Projets { get; set; }
        /// <summary>
        /// Nombre de jour travaillé dans le cram
        /// </summary>
        public decimal NbWorkedDays { get; set; }
        /// <summary>
        /// Nombre de jours de congés dans le cram
        /// </summary>
        public decimal NbHolidays { get; set; }
        /// <summary>
        /// Statut du cram
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// Transforme un cram en <code>SearchCramResultModel</code>
        /// </summary>
        /// <param name="cram"></param>
        /// <returns></returns>
        public static SearchCramResultModel FromCram(Cram cram)
        {
            return new SearchCramResultModel
            {
                Date = string.Format("{0} {1}", CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[cram.Month - 1].Capitalize(), cram.Year),
                Id = cram.Id,
                UserName = string.Format("{0} {1}", cram.User.LastName.ToUpper(), cram.User.FirstName),
                Projets = string.Join(", ", cram.CramLines
                    .Where(cl => !cl.Activity.ActivityType.IsHoliday)
                    .Select(cl => cl.Activity.Name).Distinct().OrderBy(s => s)),
                NbWorkedDays = cram.CramLines
                    .Where(line => !line.Activity.ActivityType.IsHoliday)
                    .Sum(line => line.Cram_DailyActivities.Sum(da => (decimal)da.Value)),
                NbHolidays = cram.CramLines
                    .Where(line => line.Activity.ActivityType.IsHoliday)
                    .Sum(line => line.Cram_DailyActivities.Sum(da => (decimal)da.Value)),
                Status = ((CramStatus)cram.Status).GetLabel()
            };
        }
    }
}